package libnetdb.dm.assoc.dhp.demo;

import java.util.*;
import java.text.*;
import java.io.*;

import libnetdb.dm.assoc.base.*;
import libnetdb.dm.assoc.dhp.*;
import libnetdb.dm.assoc.util.*;

public class DemoApp {
    private static ArrayList indexList = new ArrayList();
    {
        IndexManager manager = IndexManager.getInstance();
        Iterator it = manager.builderIterator();
        while(it.hasNext())
            indexList.add(((IndexBuilder)it.next()).createIndex());        
    }

    private static void printUsage(PrintStream ps){
        ps.println("usage: filename support confidence");
    }
    
    public static void listRules(Iterator ruleIterator){
        DecimalFormat format = new DecimalFormat("#0.000");
        while(ruleIterator.hasNext()){
            Rule rule = (Rule) ruleIterator.next();
            System.out.print(rule.toString());
            for(int i=0;i<indexList.size();++i){
                RuleIndex index = (RuleIndex) indexList.get(i);
                System.out.print(", " + 
                        format.format(index.getIndex(rule)));
            }
            System.out.println();                   
        }        
    }
    
    public static void main(String argv[]) throws Exception{
        if(argv.length < 3){
        	System.err.println("Too few arguments"); // Add by Silver 2003/12/26
            printUsage(System.err);
            System.exit(-1);
        }
        
		// Add by Silver 2003/12/26
		if(argv.length > 3){
			System.err.println("Too many arguments"); 
			printUsage(System.err);
			System.exit(-1);
		}     
        
		// Add by Silver 2003/12/26       
        double support = 0;
        double confidence = 0;
       
        
		// Add by Silver 2003/12/26
		try {
	        support = Double.parseDouble(argv[1]);
        } catch (NumberFormatException e) {
			System.err.println("format error in argument 'support'");
			return;
		}
		
		// Add by Silver 2003/12/26
		try {
			confidence = Double.parseDouble(argv[2]);
		} catch (NumberFormatException e) {
			System.err.println("format error in argument 'confidence'");
			return;
		}		
		
        NameSpace ns = new BasicNameSpace();
        TextInput input = new TextInput(argv[0], ns);
        DirectBuilder output = new DirectBuilder();
        DHPMiner miner = new DHPMiner(input, output, support);
        miner.addProgressListener(new ConsoleMessenger());
        miner.start();
        miner.join();
        RuleSet rs = output.getRuleSet();
        rs.setNameSpace(ns);
        
        Iterator it = rs.ancensterIterator(new Itemset(new int[0]));
        while(it.hasNext()){
            Litemset litemset = (Litemset) it.next();
            System.out.println(
                    rs.toString(litemset) + ": " + litemset.count);
        }

        System.out.println("All Rules: ");
        listRules(rs.getRules(support, confidence));
        
        Itemset itemset = new Itemset(
                new int[]{ 0, 1, 2, 3, 4, 39, 37});
        System.out.print("Recommend Rules for: ");
        for(int i=0;i<itemset.ids.length;++i)
            System.out.print(" " + ns.getName(itemset.ids[i]));
        System.out.println();
        listRules(rs.getRecommendRules(
                new int[]{0, 1, 2, 3, 4, 39, 37}, 0.02, 0.3));
    }
}